/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
package com.baselet.element.old.activity;

import java.awt.Graphics2D;
import java.awt.Point;

import com.baselet.control.enums.Direction;

public class Connector {

	private Element e1, e2;
	private Graphics2D graphics;

	public static void drawArrow(Graphics2D graphics, float zoom, int x1, int y1, int x2, int y2) {

		double ax1, ax2, ay1, ay2;
		int ax_1, ax_2, ay_1, ay_2;
		double angle = Math.atan2(y2 - y1, x2 - x1);
		ax1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) - (int) (7 * zoom);
		ax2 = ax1;
		ay1 = -(int) (4 * zoom);
		ay2 = (int) (5 * zoom);

		ax_1 = (int) (Math.cos(angle) * ax1 - Math.sin(angle) * ay1) + x1;
		ay_1 = (int) (Math.sin(angle) * ax1 + Math.cos(angle) * ay1) + y1;
		ax_2 = (int) (Math.cos(angle) * ax2 - Math.sin(angle) * ay2) + x1;
		ay_2 = (int) (Math.sin(angle) * ax2 + Math.cos(angle) * ay2) + y1;

		graphics.drawLine(x1, y1, x2, y2);
		graphics.drawLine(x2, y2, ax_1, ay_1);
		graphics.drawLine(x2, y2, ax_2, ay_2);
	}

	public Connector(Graphics2D g, Element e1, Element e2) {
		graphics = g;
		this.e1 = e1;
		this.e2 = e2;
	}

	public void paint() {
		if (e1.connectOut() && e2.connectIn()) {
			Point c2 = e2.getConnect(Direction.UP);
			Point c1 = e1.getConnect(Direction.DOWN);

			if (c1 != null && c2 != null) {
				if (!c1.equals(c2)) {
					if (e1.arrowOut() && e2.arrowIn()) {
						float zoom = e1.getHandler().getZoomFactor();
						Connector.drawArrow(graphics, zoom, c1.x, c1.y, c2.x, c2.y);
					}
					else {
						graphics.drawLine(c1.x, c1.y, c2.x, c2.y);
					}
				}
			}
		}
	}
}
